﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using BattleCity.GameLogic;

namespace BattleCity.Rendering
{

    public class BonusModel
    {
        public VertexBuffer vertexBuffer;
        public IndexBuffer indexBuffer;

        public BonusModel(GraphicsDevice graphicsDevice, Vector2 UVstart, Vector2 UVend)
        {
            vertexBuffer = new VertexBuffer(graphicsDevice, VertexPositionNormalTexture.VertexDeclaration, 20, BufferUsage.None);
            indexBuffer = new IndexBuffer(graphicsDevice, IndexElementSize.SixteenBits, 30, BufferUsage.None);

            VertexPositionNormalTexture[] vertexData = new VertexPositionNormalTexture[20];

            //front face
            vertexData[0] = new VertexPositionNormalTexture(new Vector3(-0.8f, -0.8f, +0.8f), Vector3.UnitZ, new Vector2(UVstart.X, UVend.Y));
            vertexData[1] = new VertexPositionNormalTexture(new Vector3(-0.8f, 0.8f, +0.8f), Vector3.UnitZ, new Vector2(UVstart.X, UVstart.Y));
            vertexData[2] = new VertexPositionNormalTexture(new Vector3(+0.8f, 0.8f, +0.8f), Vector3.UnitZ, new Vector2(UVend.X, UVstart.Y));
            vertexData[3] = new VertexPositionNormalTexture(new Vector3(+0.8f, -0.8f, +0.8f), Vector3.UnitZ, new Vector2(UVend.X, UVend.Y));

            //right face
            vertexData[4] = new VertexPositionNormalTexture(new Vector3(+0.8f, -0.8f, +0.8f), Vector3.UnitX, new Vector2(UVstart.X, UVend.Y));
            vertexData[5] = new VertexPositionNormalTexture(new Vector3(+0.8f, 0.8f, +0.8f), Vector3.UnitX, new Vector2(UVstart.X, UVstart.Y));
            vertexData[6] = new VertexPositionNormalTexture(new Vector3(+0.8f, 0.8f, -0.8f), Vector3.UnitX, new Vector2(UVend.X, UVstart.Y));
            vertexData[7] = new VertexPositionNormalTexture(new Vector3(+0.8f, -0.8f, -0.8f), Vector3.UnitX, new Vector2(UVend.X, UVend.Y));

            //left face
            vertexData[8] = new VertexPositionNormalTexture(new Vector3(-0.8f, -0.8f, +0.8f), Vector3.UnitX, new Vector2(UVstart.X, UVend.Y));
            vertexData[9] = new VertexPositionNormalTexture(new Vector3(-0.8f, 0.8f, +0.8f), Vector3.UnitX, new Vector2(UVstart.X, UVstart.Y));
            vertexData[10] = new VertexPositionNormalTexture(new Vector3(-0.8f, 0.8f, -0.8f), Vector3.UnitX, new Vector2(UVend.X, UVstart.Y));
            vertexData[11] = new VertexPositionNormalTexture(new Vector3(-0.8f, -0.8f, -0.8f), Vector3.UnitX, new Vector2(UVend.X, UVend.Y));

            //up face
            vertexData[12] = new VertexPositionNormalTexture(new Vector3(-0.8f, 0.8f, +0.8f), Vector3.UnitY, new Vector2(UVstart.X, UVend.Y));
            vertexData[13] = new VertexPositionNormalTexture(new Vector3(-0.8f, 0.8f, -0.8f), Vector3.UnitY, new Vector2(UVstart.X, UVstart.Y));
            vertexData[14] = new VertexPositionNormalTexture(new Vector3(+0.8f, 0.8f, -0.8f), Vector3.UnitY, new Vector2(UVend.X, UVstart.Y));
            vertexData[15] = new VertexPositionNormalTexture(new Vector3(+0.8f, 0.8f, +0.8f), Vector3.UnitY, new Vector2(UVend.X, UVend.Y));

            //back face
            vertexData[16] = new VertexPositionNormalTexture(new Vector3(-0.8f, -0.8f, -0.8f), -Vector3.UnitZ, new Vector2(UVstart.X, UVend.Y));
            vertexData[17] = new VertexPositionNormalTexture(new Vector3(-0.8f, 0.8f, -0.8f), -Vector3.UnitZ, new Vector2(UVstart.X, UVstart.Y));
            vertexData[18] = new VertexPositionNormalTexture(new Vector3(+0.8f, 0.8f, -0.8f), -Vector3.UnitZ, new Vector2(UVend.X, UVstart.Y));
            vertexData[19] = new VertexPositionNormalTexture(new Vector3(+0.8f, -0.8f, -0.8f), -Vector3.UnitZ, new Vector2(UVend.X, UVend.Y));


            short[] indexData = 
                          {
                           0,1,2,2,3,0,         //front face
                           4,5,6,6,7,4,         //right face
                           8,11,10,10,9,8,      //left face
                           12,13,14,14,15,12,   //up face
                           19,18,17,17,16,19
                          };

            indexBuffer.SetData<short>(indexData);
            vertexBuffer.SetData<VertexPositionNormalTexture>(vertexData);
        }

    }

    public class BonusRenderer
    {
        BonusModel shieldBonusModel;
        BonusModel powerBonusModel;
        BonusModel nuclearBonusModel;
        BonusModel lifeBonusModel;

        GraphicsDevice graphicsDevice;

        float rotation;

        public BonusRenderer(GraphicsDevice graphicsDevice)
        {
            rotation = 0.0f;

            this.graphicsDevice = graphicsDevice;

            shieldBonusModel = new BonusModel(graphicsDevice, new Vector2(0.0f, 0.5f), new Vector2(0.5f, 1.0f));
            powerBonusModel = new BonusModel(graphicsDevice, new Vector2(0.5f, 0.5f), new Vector2(1.0f, 1.0f));
            nuclearBonusModel = new BonusModel(graphicsDevice, new Vector2(0.5f, 0.0f), new Vector2(1.0f, 0.5f));
            lifeBonusModel = new BonusModel(graphicsDevice, new Vector2(0.0f, 0.0f), new Vector2(0.5f, 0.5f));
        }


        public void Draw(ref BasicEffect effect, GameTime gameTime)
        {
            rotation += 5.0f * (float)gameTime.ElapsedGameTime.TotalSeconds;

            foreach (Tuple<Types.Bonus, FMath.Vector2, double> bonusState in GameState.gameState.BonusList.Value)
            {
                effect.World = Matrix.CreateRotationY(rotation) *  Matrix.CreateTranslation(bonusState.Item2.xnaVector3YView + new Vector3(0,2,0));
                effect.CurrentTechnique.Passes[0].Apply();

                graphicsDevice.Indices = nuclearBonusModel.indexBuffer;

                if (bonusState.Item1.IsNuclear)
                {
                    graphicsDevice.SetVertexBuffer(nuclearBonusModel.vertexBuffer);
                    graphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 20, 0, 10);
                }
                else if (bonusState.Item1.IsWeaponUp)
                {
                    graphicsDevice.SetVertexBuffer(powerBonusModel.vertexBuffer);
                    graphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 20, 0, 10);
                }
                else if (bonusState.Item1.IsShield)
                {
                    graphicsDevice.SetVertexBuffer(shieldBonusModel.vertexBuffer);
                    graphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 20, 0, 10);
                }
                else if (bonusState.Item1.IsLife)
                {
                    graphicsDevice.SetVertexBuffer(lifeBonusModel.vertexBuffer);
                    graphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 20, 0, 10);
                }
            }
        }


    }
}
